package com.qqtech.qquan.pub.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.BizzLevel1Enum;
import com.qqtech.core.common.enums.BizzLevel2Enum;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.activity.model.ActivityBaseVo;
import com.qqtech.qquan.activity.service.ActivityBaseService;
import com.qqtech.qquan.circle.model.CircleTopic;
import com.qqtech.qquan.circle.service.CircleMemberService;
import com.qqtech.qquan.circle.service.CircleTopicService;
import com.qqtech.qquan.community.model.CommunityMessage;
import com.qqtech.qquan.community.model.CommunityMessageVo;
import com.qqtech.qquan.community.service.CommunityMessageService;
import com.qqtech.qquan.hi.service.HiCircleService;
import com.qqtech.qquan.hi.service.HiCommunityService;
import com.qqtech.qquan.pub.enums.PubPraiseStatusDictEnum;
import com.qqtech.qquan.pub.model.PubPraise;
import com.qqtech.qquan.pub.model.PubPraiseQuery;
import com.qqtech.qquan.pub.model.PubPraiseVo;
import com.qqtech.qquan.pub.service.PubPraiseService;
import com.qqtech.qquan.push.service.PushBizzService;
import com.qqtech.qquan.sys.enums.SysConst;
import com.qqtech.qquan.user.model.UserBase;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.model.UserTopic;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.qquan.user.service.UserTopicService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/pub/praise")
public class PubPraiseController extends BaseWebController<PubPraise, PubPraiseQuery> {
	@Resource
	private PubPraiseService pubPraiseService;
	@Resource
	private UserBaseService userBaseService;
	@Resource
	private CircleTopicService circleTopicService;
	@Resource
	private CircleMemberService circleMemberService;
	@Resource
	private CommunityMessageService communityMessageService;
	@Resource
	private UserTopicService userTopicService;
	@Resource
	private PushBizzService pushBizzService;
	@Resource
	private ActivityBaseService activityBaseService;
	@Resource
	private HiCircleService hiCircleService;
	@Resource
	private HiCommunityService hiCommunityService;

	@Override
	protected BaseService<PubPraise> getBaseService() {
		return pubPraiseService;
	}

	/**
	 * 我的点赞
	 * 
	 * @param pubCommentQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/manage/page")
	@ResponseBody
	public Result managePage(@RequestBody PubPraiseQuery pubPraiseQuery) {
		Result result = new Result();
		UserBaseVo userVo = null;
		Integer curUserId = super.getUserId();
		pubPraiseQuery.setCreatorId(curUserId);
		pubPraiseQuery.setStatus(PubPraiseStatusDictEnum.NORMAL.getCode());
		Pageable pageKit = new PageKit(pubPraiseQuery.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<PubPraiseVo> page = pubPraiseService.queryPageList(pubPraiseQuery, pageKit);
		List<PubPraiseVo> praiseList = page.getContent();
		if (praiseList != null && praiseList.size() > 0) {
			for (PubPraiseVo praise : page.getContent()) {
				if (BizzLevel2Enum.CIRCLE_TOPIC.getCode().equals(praise.getBizzType())) {
					CircleTopic topic = circleTopicService.getById(praise.getBizzId());
					if (topic != null) {
						praise.setBizzContent(topic.getContent());
						praise.setBizzImg(topic.getImg());
					}
				} else if (BizzLevel2Enum.COMMUNITY_MESSAGE.getCode().equals(praise.getBizzType())) {
					CommunityMessage msg = communityMessageService.getById(praise.getBizzId());
					if (msg != null) {
						praise.setBizzContent(msg.getContent());
						praise.setBizzImg(msg.getImg());
					}
				} else if (BizzLevel2Enum.USER_TOPIC.getCode().equals(praise.getBizzType())) {
					UserTopic userTopic = userTopicService.getById(praise.getBizzId());
					if (userTopic != null) {
						praise.setBizzContent(userTopic.getContent());
						praise.setBizzImg(userTopic.getImg());
					}
				}
				praise.setImgString(praise.getBizzImg(),SysConst.IMG_POSTFIX);
				if(praise.getBeUserId()!=null){
					userVo = userBaseService.getById(praise.getBeUserId());
					if(userVo !=null){
						praise.setBeUserImg(userVo.getLogo());
						praise.setBeUserName(userVo.getNick());
					}
				}
			}
		}

		result.put("page", page);
		return result;
	}

	/**
	 * 点赞
	 * 
	 * @param pubConcern
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/doAdd")
	@ResponseBody
	public Result doAdd(@RequestBody PubPraiseQuery pubPraise) {
		Result result = new Result();
		// 查看是否已存在点赞信息
		pubPraise.setCreatorId(super.getUserId());
		pubPraise.setStatus(PubPraiseStatusDictEnum.NORMAL.getCode());
		PubPraise temp = pubPraiseService.getOne(pubPraise);
		if (temp == null) {
			if(pubPraise.getBizzType().equalsIgnoreCase(BizzLevel2Enum.CIRCLE_TOPIC.getCode())){
				//如果是圈子话题，要判断该用户是否圈子的成员
				CircleTopic circleTopic = circleTopicService.getById(pubPraise.getBizzId());
				if(circleTopic!=null && circleTopic.getCircleId()!=null){
					if(!circleMemberService.isCircleMember(getUserId(), circleTopic.getCircleId())){
						result.addErr("你不是圈子成员!");
						return result;
					}
				}else{
					result.addErr("找不到该圈子话题!");
					return result;
				}
				circleTopicService.increasePraise(pubPraise.getBizzId(), 1);//圈子话题点赞数量+1
				if(circleTopic.getCreatorId()!=super.getUserId().intValue()){
					//荣誉系统-ryz
					hiCircleService.praiseTopic(super.getUserId(), pubPraise.getBizzId());
				}
			}else if(pubPraise.getBizzType().equalsIgnoreCase(BizzLevel2Enum.COMMUNITY_MESSAGE.getCode())){
				if (pubPraise.getCommunityId() != null) {
					UserBase userBase = userBaseService.getById(super.getUserId());
					if (userBase != null) {
						if (userBase.getCommunityId() == null
								|| pubPraise.getCommunityId().intValue() != userBase.getCommunityId().intValue()) {
							result.addErr("请加入该社区后再给邻里点赞");
							return result;
						}
					}
				}
				communityMessageService.increasePraise(pubPraise.getBizzId(), 1);//社区大喇叭点赞数量+1
				CommunityMessageVo communityMessageVo=communityMessageService.getById(pubPraise.getBizzId());
				if(communityMessageVo.getCreatorId()!=super.getUserId().intValue()){
					//荣誉系统-ryz
					hiCommunityService.praiseMessage(super.getUserId(), pubPraise.getBizzId());
				}
			}
			pubPraise.setCreatorId(super.getUserId());
			Integer beUserId = pubPraiseService.getBeUserId(pubPraise.getBizzId(), pubPraise.getBizzType());
			pubPraise.setBeUserId(beUserId);
			pubPraiseService.insert(pubPraise);
			String returnStr = addPraiseUserList(pubPraise);
			result.put("praiseUserList", returnStr);
			result.setCode(RespEnum.OK.getCode());
			pushBizzService.pubPraise(super.getUserId(), beUserId);
			if(pubPraise.getBizzType().equalsIgnoreCase(BizzLevel2Enum.USER_TOPIC.getCode())){
				userTopicService.increasePraise(pubPraise.getBizzId(), 1);//友事评论数量+1
			}
		} else {
			// 返回提示信息
			result.addErr("已点赞");
		}
		return result;
	}

	/**
	 * 取消点赞
	 * 
	 * @param pubConcern
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/doCancle")
	@ResponseBody
	public Result doCancle(@RequestBody PubPraiseQuery pubPraise) {
		Result result = new Result();
		// 查看是否已存在点赞信息
		pubPraise.setCreatorId(super.getUserId());
		pubPraise.setStatus(PubPraiseStatusDictEnum.NORMAL.getCode());
		PubPraise temp = pubPraiseService.getOne(pubPraise);
		if (temp != null) {
			temp.setStatus(PubPraiseStatusDictEnum.DELETE.getCode());
			pubPraiseService.updateById(temp);
			if (pubPraise.getBizzId() != null) {
				String returnStr = addPraiseUserList(pubPraise);
				result.put("praiseUserList", returnStr);
				if(temp.getBizzType().equalsIgnoreCase(BizzLevel2Enum.CIRCLE_TOPIC.getCode())){
					circleTopicService.increasePraise(temp.getBizzId(), -1);//圈子话题点赞数量-1
				}
				if(pubPraise.getBizzType().equalsIgnoreCase(BizzLevel2Enum.USER_TOPIC.getCode())){
					userTopicService.increasePraise(pubPraise.getBizzId(), -1);//友事评论数量-1
				}
				if(pubPraise.getBizzType().equalsIgnoreCase(BizzLevel2Enum.COMMUNITY_MESSAGE.getCode())){
					communityMessageService.increasePraise(pubPraise.getBizzId(), -1);//社区大喇叭点赞数量-1
				}
			}
			result.setCode(RespEnum.OK.getCode());
		} else {
			// 返回提示信息
			result.addErr("已取消点赞");
		}
		return result;
	}

	/**
	 * 返回点赞状态
	 * 
	 */
	@RequestMapping(value = "/getPubPraise")
	@ResponseBody
	public Result getPubPraise(@RequestBody PubPraiseQuery pubPraise) {
		Result result = new Result();
		// 查看是否已存在点赞信息
		PubPraise praise = pubPraiseService.getOne(pubPraise);
		if (praise != null) {
			result.put("praise", praise);
		}
		return result;
	}

	// 点赞人列表
	private String addPraiseUserList(PubPraise pubPraise) {
		List<PubPraise> praiseList = pubPraiseService.queryPraiseListByBizz(pubPraise.getBizzId(),
				pubPraise.getBizzType());
		String praiseUserList = "";
		for (int i = 0; i < praiseList.size(); i++) {
			PubPraise praise = praiseList.get(i);
			UserBase user = userBaseService.getById(praise.getCreatorId());
			if (i == praiseList.size() - 1) {
				praiseUserList = praiseUserList + user.getNick();
			} else {
				praiseUserList = praiseUserList + user.getNick() + ",";
			}
		}
		return praiseUserList;
	}

	/**
	 * 他人对我的评论 分页显示
	 * 
	 * @param pubCommentQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/manage/praise2me")
	@ResponseBody
	public Result praise2me(@RequestBody PubPraiseQuery praiseQuery) {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		praiseQuery.setBeUserId(curUserId);
		Integer lastCount = praiseQuery.getCount();
		praiseQuery.setStatus(PubPraiseStatusDictEnum.NORMAL.getCode());
		Pageable pageKit = new PageKit(1, lastCount, new PageOrder(Direction.DESC, "id"));
		List<PubPraiseVo> praiseList = pubPraiseService.queryList(praiseQuery, pageKit);
		if (praiseList != null && praiseList.size() > 0) {
			UserBaseVo userVo = null;// 用于取用户呢称、头像
			for (PubPraiseVo praise : praiseList) {
				userVo = userBaseService.getById(praise.getCreatorId());
				if(userVo!=null){
					praise.setCreatorName(userVo.getNick());
					praise.setCreatorImg(userVo.getLogo());
				}
				if (BizzLevel2Enum.CIRCLE_TOPIC.getCode().equals(praise.getBizzType())) {
					CircleTopic topic = circleTopicService.getById(praise.getBizzId());
					if (topic != null) {
						praise.setBizzContent(topic.getContent());
						praise.setBizzImg(topic.getImg());
					}
				} else if (BizzLevel2Enum.COMMUNITY_MESSAGE.getCode().equals(praise.getBizzType())) {
					CommunityMessage msg = communityMessageService.getById(praise.getBizzId());
					if (msg != null) {
						praise.setBizzContent(msg.getContent());
						praise.setBizzImg(msg.getImg());
					}
				} else if (BizzLevel1Enum.ACTIVITY.getCode().equals(praise.getBizzType())) {
					ActivityBaseVo activityBaseVo = activityBaseService.getById(praise.getBizzId());
					if (activityBaseVo != null) {
						praise.setBizzContent(activityBaseVo.getName());
						praise.setBizzImg(activityBaseVo.getLogo());
					}
				}
				praise.setImgString(praise.getBizzImg(), SysConst.IMG_POSTFIX);
			}
		}
		result.put("praiseList", praiseList);
		return result;
	}

	/**
	 * 他人对我的"新的点赞"
	 * 
	 * @param praiseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/manage/praiseNew2me")
	@ResponseBody
	public Result praiseNew2me(@RequestBody PubPraiseQuery praiseQuery) {
		Result result = new Result();
		if (praiseQuery.getStartAt() == null) {
			result.put("newPraiseCount", 0);
			return result;
		}
		praiseQuery.setStatus(PubPraiseStatusDictEnum.NORMAL.getCode());
		praiseQuery.setBeUserId(super.getUserId());
		int newPraiseCount = pubPraiseService.queryCount(praiseQuery);
		result.put("newPraiseCount", newPraiseCount);
		return result;
	}

	/**
	 * 获取点赞的业务标题
	 * 
	 * @param praise
	 * @return
	 */
	private String getPraiseBizzTitle(PubPraiseVo praise) {
		String bizzType = praise.getBizzType();
		if (praise != null) {
			if (bizzType.equalsIgnoreCase(BizzLevel2Enum.CIRCLE_TOPIC.getCode())) {
				CircleTopic topic = circleTopicService.getById(praise.getBizzId());
				return topic != null ? topic.getContent() : "";
			} else if (bizzType.equalsIgnoreCase(BizzLevel2Enum.USER_TOPIC.getCode())) {
				UserTopic topic = userTopicService.getById(praise.getBizzId());
				return topic != null ? topic.getContent() : "";
			} else if (bizzType.equalsIgnoreCase(BizzLevel2Enum.COMMUNITY_MESSAGE.getCode())) {
				CommunityMessage message = communityMessageService.getById(praise.getBizzId());
				return message != null ? message.getContent() : "";
			}
		}

		return "";
	}
}
